package com.caigou.component;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class CaigouIdComponent {
    @Resource
    private RedisObjectComponent redisObjectComponent;

    public Long getCaigouIdCompnent(){
        StringBuffer sb = new StringBuffer();
        Date current = new Date(System.currentTimeMillis());
        sb.append(DateUtil.format(current,"yyyyMMddHHmmss"));
        log.info("caigouId's date字段={}", sb.toString());
        String caigouIdKey = "caigou:cms:idKey";
        Integer caigouId = (Integer)redisObjectComponent.get(caigouIdKey);
        if(caigouId!=null && caigouId.intValue()>=9999L){
            redisObjectComponent.del(caigouIdKey);
        }
        Long lCaigouId = redisObjectComponent.incr(caigouIdKey, 1L);//原子操作
        caigouId = new Long(lCaigouId).intValue();
        log.info("caigouId's 自增id字段={}", caigouId);
        java.text.DecimalFormat format=new java.text.DecimalFormat("0000");
        String strCaigouId = format.format(caigouId);
        sb.append(strCaigouId);
        log.info("caigouId 整体结果为{}", sb.toString());
        return Long.parseLong(sb.toString());
    }
}
